package com.ai.tool;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;

public class HttpUtil {

	private static HttpURLConnection buildHttpConn(String httpUrl) throws IOException {
		Integer timeout = 1000;
		URL url = new URL(httpUrl);
		HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
		urlConn.setDoOutput(true);
		urlConn.setDoInput(true);
		urlConn.setRequestMethod("POST");
		urlConn.setConnectTimeout(timeout);
		return urlConn;
	}

	public static String send(String httpUrl, String text) throws Exception {
		OutputStream out = null;
		InputStream inFile = null;
		InputStream in = null;
		BufferedReader reader = null;
		HttpURLConnection urlConn = null;
		try {
			urlConn = buildHttpConn(httpUrl);
			// 参数
			out = urlConn.getOutputStream();
			inFile = new FileInputStream(text);
			int len = -1;
			byte[] bt = new byte[20];
			while ((len = inFile.read(bt)) != -1) {
				out.write(bt, 0, len);
			}
			out.flush();
			if (urlConn.getContentLength() == -1) {
				throw new Exception("http接收无返回结果");
			}
			if (urlConn.getResponseCode() != 200) {
				throw new Exception("http返回失败，错误码" + urlConn.getResponseCode());
			}
			in = urlConn.getInputStream();
			reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			StringBuffer bf = new StringBuffer();
			String temp = "";
			while ((temp = reader.readLine()) != null) {
				bf.append(temp);
			}
			urlConn.disconnect();
			return bf.toString();
		} catch (SocketTimeoutException e) {
			// retry();
			throw new Exception("文件传输失败 ", e);
		} catch (ConnectException e) {
			// retry();
			throw new Exception("文件传输失败 ", e);
		} catch (IOException e) {
			throw new Exception("文件传输失败 ", e);
		} finally {
			close(out);
			close(inFile);
			close(reader);
			close(in);
			close(urlConn);
		}
	}

	public static void close(HttpURLConnection urlConn) {
		try {
			if (urlConn != null) {
				urlConn.disconnect();
			}
		} catch (Exception e) {
			// e.printStackTrace();
		}
	}

	public static void close(InputStream st) {
		try {
			if (st != null)
				st.close();
		} catch (IOException e) {
			// e.printStackTrace();
		}
	}

	public static void close(OutputStream st) {
		try {
			if (st != null)
				st.close();
		} catch (IOException e) {
			// e.printStackTrace();
		}
	}

	public static void close(Reader st) {
		try {
			if (st != null)
				st.close();
		} catch (IOException e) {
			// e.printStackTrace();
		}
	}

	public static void close(Writer st) {
		try {
			if (st != null)
				st.close();
		} catch (IOException e) {
			// e.printStackTrace();
		}
	}

}
